package fyp.mobile;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.HashMap;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import view.Core.R;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class AddCommentActivity extends Activity {

	TextView mTextView01, mTextView02, mTextView05;
	EditText name, comment;
	Button mButton;

	String ID, CID;

	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	String date = sdf.format(new Date(0));

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.addcomment);

		findViewById();

		EditText name = (EditText) findViewById(R.id.name);
		selectCommentIDFromDB();

		Bundle bundle = this.getIntent().getExtras();
		ID = bundle.getString("treeID");

	}

	public void onClickAddCommentButton(View view) {
		EditText name = (EditText) findViewById(R.id.name);
		EditText comment = (EditText) findViewById(R.id.comment);

		addNewComment(CID, name.getText().toString(), comment.getText()
				.toString().replace(' ', ',' ), date + "", ID);
	}

	private void addNewComment(String addCID, String addname,
			String addcomment, String adddate, String addID) {
		// ///http get
		
		String url = getResources().getString(R.string.serverURL)
				+ "insertComment.php?commentID=" + addCID + "&Name=" + addname
				+ "&comment=" + addcomment + "&date=" + adddate + "&treeID="
				+ addID;

		try {
			HttpClient client = new DefaultHttpClient();
			HttpGet request = new HttpGet(url);
			HttpResponse response = client.execute(request);

			String line = "";
			String reply = "";
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					response.getEntity().getContent()));
			while ((line = reader.readLine()) != null) {
				reply += line;
			}
			reader.close();
			errorToast(reply);

			Intent intent = new Intent();
			intent.putExtra("treeID", addID);
			setResult(RESULT_OK, intent);
			
			finish();

		} catch (Exception e) {
			/*
			String[] names = addcomment.split(" ");
			for(String name:names){
				    System.out.println(name);
				    errorToast( name + "_");
				}
				
				*/
			errorToast(e+"");
		}

	}
	

	public void selectCommentIDFromDB() {

		// ///http get
		String url = getResources().getString(R.string.serverURL)
				+ "commentID.php";

		// String url = "http://192.168.132.155/oldTreeWeb/commentID.php";
		try {
			HttpClient client = new DefaultHttpClient();
			HttpGet request = new HttpGet(url);
			HttpResponse response = client.execute(request);

			String line = "";
			String reply = "";
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					response.getEntity().getContent()));
			while ((line = reader.readLine()) != null) {
				reply = line;
			}
			reader.close();
			CID = reply;

		} catch (Exception e) {
			errorToast("" + e);
		}

	}

	public void errorToast(String msg) {
		Toast.makeText(this, "" + msg, Toast.LENGTH_SHORT).show();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		super.onCreateOptionsMenu(menu);
		menu.add(Menu.NONE, Menu.FIRST + 1, Menu.NONE, "Back").setShortcut('0',
				'a');
		menu.add(Menu.NONE, Menu.FIRST + 2, Menu.NONE, "Help").setShortcut('1',
				'b');
		menu.add(Menu.NONE, Menu.FIRST + 3, Menu.NONE, "Quit").setShortcut('2',
				'c');
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		String title = item.getTitle().toString();
		switch (item.getItemId()) {

		case Menu.FIRST + 1:
			finish();
			break;
		case Menu.FIRST + 2:
			Toast.makeText(this, "Your Choice: " + title, Toast.LENGTH_LONG)
					.show();
			break;
		case Menu.FIRST + 3:
			show_alert();
			break;
		}

		return super.onOptionsItemSelected(item);

	}

	private void show_alert() {

		AlertDialog.Builder alert_box = new AlertDialog.Builder(this);
		alert_box.setMessage("Do you want to quit the app?");
		alert_box.setPositiveButton("Yes",
				new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						onDestroy();
					}
				});

		alert_box.setNegativeButton("No",
				new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
					}
				});

		alert_box.show();
	}

	
	public void onDestroy() {
		super.onDestroy();
		android.os.Process.killProcess(android.os.Process.myPid());
	}
	

	public void findViewById() {

		TextView mTextView01 = (TextView) findViewById(R.id.TextView01);
		EditText name = (EditText) findViewById(R.id.name);
		TextView mTextView02 = (TextView) findViewById(R.id.TextView02);
		EditText comment = (EditText) findViewById(R.id.comment);
		TextView mTextView05 = (TextView) findViewById(R.id.TextView05);

		Button mButton = (Button) findViewById(R.id.AddCommentButton);
	}
}